﻿using UnityEngine;
using System.Collections;
using UniLua;

namespace TCG.FFI
{
	public static class UnityAudioSourceLib
	{
		public static string LIB_NAME = "UnityAudioSourceLib";

		public static string ParentLibName = "UnityBehaviourLib";

		public static int mf_GetBypassEffects (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushBoolean (audioSource.bypassEffects);
			return 1;
		}

		public static int mf_SetBypassEffects (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.bypassEffects = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_GetClip (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			if (audioSource.clip == null)
			{
				luaState.PushNil ();
			}
			else
			{
				LuaTools.L_PushUserData (luaState, audioSource.clip, UnityAudioClipLib.LIB_NAME);
			}
			return 1;
		}

		public static int mf_SetClip (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.clip = LuaTools.L_CheckUData (luaState, 2, UnityAudioClipLib.LIB_NAME) as AudioClip;
			return 0;
		}

		public static int mf_GetDopplerLevel (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.dopplerLevel);
			return 1;
		}

		public static int mf_SetDopplerLevel (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.dopplerLevel = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetIgnoreListenerPause (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushBoolean (audioSource.ignoreListenerPause);
			return 1;
		}

		public static int mf_SetIgnoreListenerPause (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.ignoreListenerPause = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_GetIgnoreListenerVolume (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushBoolean (audioSource.ignoreListenerVolume);
			return 1;
		}


		public static int mf_SetIgnoreListenerVolume (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.ignoreListenerVolume = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_IsPlaying (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushBoolean (audioSource.isPlaying);
			return 1;
		}

		public static int mf_IsLooping (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushBoolean (audioSource.loop);
			return 1;
		}

		public static int mf_SetLooping (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.loop = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_GetMaxDistance (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.maxDistance);
			return 1;
		}

		public static int mf_SetMaxDistance (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.maxDistance = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetMinDistance (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.minDistance);
			return 1;
		}
		
		public static int mf_SetMinDistance (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.minDistance = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetMute (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushBoolean (audioSource.mute);
			return 1;
		}

		public static int mf_SetMute (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.mute = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_GetPan (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.pan);
			return 1;
		}

		public static int mf_SetPan (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.pan = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetPanLevel (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.panLevel);
			return 1;
		}

		public static int mf_SetPanLevel (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.panLevel = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetPitch (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.pitch);
			return 1;
		}

		public static int mf_SetPitch (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.pitch = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetPriority (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushInteger (audioSource.priority);
			return 1;
		}

		public static int mf_SetPriority (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.priority = luaState.ToInteger (2);
			return 0;
		}

		public static int mf_GetRolloffMode (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushInteger ((int)audioSource.rolloffMode);
			return 1;
		}

		public static int mf_SetRolloffMode (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.rolloffMode = (AudioRolloffMode)luaState.ToInteger (2);
			return 0;
		}

		public static int mf_GetSpread (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.spread);
			return 1;
		}

		public static int mf_SetSpread (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.spread = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetTime (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.time);
			return 1;
		}

		public static int mf_SetTime (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.time = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_GetTimeSamples (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushInteger (audioSource.timeSamples);
			return 1;
		}

		public static int mf_SetTimeSamples (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.timeSamples = luaState.ToInteger (1);
			return 0;
		}

		public static int mf_GetVelocityUpdateMode (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushInteger ((int)audioSource.velocityUpdateMode);
			return 1;
		}

		public static int mf_SetVelocityUpdateMode (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.velocityUpdateMode = (AudioVelocityUpdateMode)luaState.ToInteger (2);
			return 0;
		}

		public static int mf_GetVolume (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			luaState.PushNumber (audioSource.volume);
			return 1;
		}

		public static int mf_SetVolume (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.volume = (float)luaState.ToNumber (2);
			return 0;
		}

		public static int mf_Play (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.Play ();
			return 0;
		}

		public static int mf_PlayDelayed (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.PlayDelayed ((float)luaState.ToNumber (2));
			return 0;
		}

		public static int mf_PlayOneShot (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			AudioClip clip = LuaTools.L_CheckUData (luaState, 2, UnityAudioClipLib.LIB_NAME) as AudioClip;
			float volumeScale = 1.0f;
			if (luaState.GetTop () > 2)
			{
				volumeScale = (float)luaState.ToNumber (3);
			}
			audioSource.PlayOneShot (clip, volumeScale);
			return 0;
		}

		public static int mf_Stop (ILuaState luaState)
		{
			AudioSource audioSource = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as AudioSource;
			audioSource.Stop ();
			return 0;
		}

		public static int lf_PlayClipAtPoint (ILuaState luaState)
		{
			AudioClip clip = LuaTools.L_CheckUData (luaState, 1, UnityAudioClipLib.LIB_NAME) as AudioClip;
			Vector3 position = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			float volume = 1;
			if (luaState.GetTop () > 2)
			{
				volume = (float)luaState.ToNumber (3);
			}
			AudioSource.PlayClipAtPoint (clip, position, volume);
			return 0;
		}
	}
}